package com.lcicl.reggie.service.impl;
/*
  Date: 2023/10/31
  Author: Luochao
  Description:
*/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcicl.reggie.entity.AddressBook;
import com.lcicl.reggie.mapper.AddressBookMapper;
import com.lcicl.reggie.service.AddressBookService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Stream;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {

    @Override
    public boolean changeDefault(AddressBook addressBook) {

        //把该用户全部地址设为不默认
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, addressBook.getUserId());
        List<AddressBook> addressBooks = this.list(queryWrapper);
        addressBooks.forEach(address -> {
            address.setIsDefault(0);
            this.updateById(address);
        });

        //设置默认
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getId, addressBook.getId())
                .set(AddressBook::getIsDefault,1);
        boolean flag = this.update(updateWrapper);
        return flag;

    }
}
